﻿using System;
using System.Collections.Generic;
using System.Text;
using PaliTextReader2.ContentsDSTableAdapters;
using System.Windows.Forms;
using ICSharpCode.SharpZipLib.Zip;
using System.IO;

namespace PaliTextReader2.Utilities
{
    public class Utility
    {
        static char[] arrFind = new char[] { 'ā', 'ī', 'ū', 'ṭ', 'ñ', 'ṃ', 'ṇ', 'ṅ', 'ḷ', 'ḍ', 'Ā', 'Ī', 'Ū', 'Ṭ', 'Ñ', 'Ḍ' };
        static char[] arrReplace = new char[] { 'a', 'i', 'u', 't', 'n', 'm', 'n', 'n', 'l', 'd', 'A', 'I', 'U', 'T', 'N', 'D' };
        static string[] arrReplaceRegex = new string[] { "aāâ", "iīî", "uūû", "tṭ", "nṇṅñ", "mṃ", "n", "n", "lḷ", "dḍ", "A", "I", "U", "T", "N", "D" };

        public static string ConvertDiac(string strInput)
        {
            for (int i = 0; i < arrFind.Length; i++)
                strInput = strInput.Replace(arrFind[i], arrReplace[i]);

            return strInput;
        }

        public static string GetNodePath(int NodeID)
        {
            string strLocation = String.Empty;
            int ParentID;
            NodesTableAdapter objNodesTA = new NodesTableAdapter();
            ContentsDS.NodesDataTable dtRowTable = objNodesTA.GetNode(NodeID);
            if (dtRowTable.Rows.Count > 0)
            {
                while (!dtRowTable[0].IsParentIDNull())
                {
                    ParentID = dtRowTable[0].ParentID;
                    strLocation = dtRowTable[0].NodeTitle + @"\ " + strLocation;
                    dtRowTable = objNodesTA.GetNode(ParentID);
                }

                strLocation = dtRowTable[0].NodeTitle + @"\ " + strLocation.TrimEnd(' ').TrimEnd('\\');
            }

            return strLocation;
        }

        public static string LibraryPath
        {
            get;
            set;
        }

        public static string CanonDirectory
        {
            get
            {
                return LibraryPath + @"\canon.zip";
            }
        }

        public static string CanonIndexDirectory
        {
            get
            {
                return LibraryPath + @"\canonindex";
            }
        }

        public static string PEDDirectory
        {
            get
            {
                return LibraryPath + @"\ped.zip";
            }
        }

        public static string PEDIndexDirectory
        {
            get
            {
                return LibraryPath + @"\PEDindex";
            }
        }

        public static string ConvertToRegex(string strPattern)
        {
            //this will keep Regular Expressions anchors
            string strRetVal = "";
            bool bReplaced = false;

            //default is exact match. surround with asterisks to find all
            if (strPattern.StartsWith("*"))
                strPattern = strPattern.Substring(1);
            else
                strPattern = @"\A" + strPattern;

            if (strPattern.EndsWith("*"))
                strPattern = strPattern.Substring(0,strPattern.Length-1);
            else
                strPattern += @"\z";

            string strPatternNoDiac=ConvertDiac(strPattern).ToLower();

            for(int j=0;j<strPatternNoDiac.Length;j++)
            {
                char ch=strPatternNoDiac[j];
                bReplaced = false;

                if ((j > 0 && strPatternNoDiac[j - 1] == '\\'))
                {
                    strRetVal += strPattern[j];
                    bReplaced = true;
                }
                else
                {
                    for (int i = 0; i < arrReplace.Length; i++)
                        if (arrReplace[i] == ch)
                        {
                            strRetVal += "[" + arrReplaceRegex[i] + "]";
                            bReplaced = true;
                            break;
                        }
                }

                if(!bReplaced)
                    strRetVal += ch;
            }

            return strRetVal;
        }

        public static string GetBookContent(string strArchive,string strFileName)
        {
            //StreamReader objSR = new StreamReader(Utility.CanonDirectory + strFileName + ".htm", true);
            ZipInputStream objStream = new ZipInputStream(File.OpenRead(strArchive));
            ZipEntry theEntry;
            string strFullFileName = strFileName.ToLower() + ".htm";

            MemoryStream msOut = new MemoryStream();
            byte[] arrFileContent = null;

            while ((theEntry = objStream.GetNextEntry()) != null)
            {
                if (theEntry.Name.ToLower() == strFullFileName)
                {
                    arrFileContent = new byte[2048];

                    while (true)
                    {
                        int size = objStream.Read(arrFileContent, 0, arrFileContent.Length);
                        if (size > 0)
                        {
                            msOut.Write(arrFileContent, 0, size);
                        }
                        else
                            break;
                    }
                }
            }

            string strRetVal = String.Empty;
            byte[] arrFileBody = msOut.ToArray();
            byte[] arrPreamble=Encoding.UTF8.GetPreamble();

            if (arrFileBody[0] == arrPreamble[0] && arrFileBody[1] == arrPreamble[1] &&
                arrFileBody[2] == arrPreamble[2])
                strRetVal = Encoding.UTF8.GetString(arrFileBody, arrPreamble.Length, arrFileBody.Length - arrPreamble.Length);
            else
                strRetVal = Encoding.UTF8.GetString(arrFileBody);

            return strRetVal;
        }
    }
}
